from tkinter import *
from openai import OpenAI

class SmartAssistantGUI:
    def __init__(self, root):
        self.root = root
        self.root.title("智能旅游助手-路线规划")
        
        # 初始化OpenAI客户端
        self.client = OpenAI(base_url="http://localhost:8000/v1", api_key="not-needed")
        
        # 创建输入区域
        self.create_input_area()
        
        # 创建结果显示区域
        self.create_result_area()
    
    def create_input_area(self):
        input_frame = Frame(self.root)
        input_frame.pack(pady=10)
        
        # 起点输入框
        Label(input_frame, text="起点:").grid(row=0, column=0, sticky=W)
        self.start_point = Entry(input_frame, width=30)
        self.start_point.grid(row=0, column=1, padx=5, pady=5)
        
        # 终点输入框
        Label(input_frame, text="终点:").grid(row=1, column=0, sticky=W)
        self.end_point = Entry(input_frame, width=30)
        self.end_point.grid(row=1, column=1, padx=5, pady=5)
        
        # 提交按钮
        Button(input_frame, text="规划路线", command=self.plan_route).grid(row=2, columnspan=2, pady=10)
    
    def plan_route(self):
        start = self.start_point.get().strip()
        end = self.end_point.get().strip()
        
        if not start or not end:
            self.result_text.delete("1.0", END)
            self.result_text.insert(END, "请输入起点和终点")
            return
            
        question = f"请规划从{start}到{end}的最佳路线"
        
        completion = self.client.chat.completions.create(
            model="local-model",
            messages=[
                {"role": "system", "content": "你是一个专业的旅游路线规划助手，请提供详细合理的路线建议。"},
                {"role": "user", "content": question}
            ],
            temperature=0.7,
            stream=True,
        )
        
        self.result_text.delete("1.0", END)
        for chunk in completion:
            if chunk.choices[0].delta.content:
                self.result_text.insert(END, chunk.choices[0].delta.content)
                self.root.update()
    
    def create_result_area(self):
        result_frame = Frame(self.root)
        result_frame.pack(pady=10)
        
        Label(result_frame, text="智能助手回答:").pack()
        self.result_text = Text(result_frame, height=15, width=50)
        self.result_text.pack()

if __name__ == "__main__":
    root = Tk()
    app = SmartAssistantGUI(root)
    root.mainloop()